#ifndef __ATMOS_MasterServerDatabase_h__
#define __ATMOS_MasterServerDatabase_h__

#include "ATMOS_Common.h"

#include "libpq-fe.h"

namespace ATMOS {

//! manages and wraps all communication with the master server database
class AtmosExport MasterServerDatabase
{
public:
	MasterServerDatabase();
	virtual ~MasterServerDatabase();

	//! connect to a database with the provided information
	void connect( const DatabaseConnectionInfo &info );
	//! disconnect from the connected database
	void disconnect();
	
	//! add a game server to the master server's records
	void addConnectedGameServer( const GameServerInfo &info );
	//! add a game client tot he master server's records
	void addConnectedGameClient( const GameClientInfo &info );

	//! remove a game server from the master server's records
	void removeConnectedGameServer( const std::string &guid );
	//! remove a game client from the asmter server's records
	void removeConnectedGameClient( const std::string &guid );

	//! get information about a game server by GUID
	void getGameServerInfo( const std::string &guid, GameServerInfo &info );
	//! get information about a game client by GUID
	void getGameClientInfo( const std::string &guid, GameClientInfo &info );
	
	//! get the total number of registered game servers
	int getNumGameServers();
	//! get the number of game servers that are not full
	int getNumOpenGameServers();
		
	//! get a list of game servers, starting at offset and ending at offset + count
	void getGameServers( std::list<std::string> &serversList, unsigned int offset,
		unsigned int count );	
	//! get a list of game servers that are not full
	void getOpenGameServers( std::list<std::string> &serversList, unsigned int offset, 
		unsigned int count );
		
	bool isServer( const std::string &guid );
	//! check whether a given server is full
	bool isServerFull( const std::string &guid );
	//! check a given server's player count
	int getServerPlayerCount( const std::string &guid );
	//! check a given server's max players
	int getServerMaxPlayerCount( const std::string &guid );
	//! increment the player count for a given server (as stored by the master server, may not always be accurate)
	void incrementServerPlayerCount( const std::string &guid );
	//! decrement the player count for a given server (as stored by the master server, may not always be accurate)
	void decrementServerPlayerCount( const std::string &guid );
		
	bool isClient( const std::string &guid );
	//! get the number of registered game clients
	int getNumGameClients();
	//! get a list of game clients, starting at offset and ending at offset + count
	void getGameClients( std::list<std::string> &clientsList, unsigned int offset,
		unsigned int count );
	
private:
	//! execute the given query, results can be accessed with getResult
	void doQuery( const std::string &query, bool echoQuery = false );
	//! get the results from the last executed query (may be null)
	PGresult *getResult();
	//! clear the results of the last executed query
	void clearResult();

private:
	PGconn *mConnection;
	PGresult *mResult;
};

} // end namespace ATMOS

#endif